package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairWorkdetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.RfidDataBo;
import com.ruoyi.tb.lmt.domain.vo.RfidDataVo;
import com.ruoyi.tb.lmt.domain.RfidData;
import com.ruoyi.tb.lmt.mapper.RfidDataMapper;
import com.ruoyi.tb.lmt.service.IRfidDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * rfid信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@RequiredArgsConstructor
@Service
public class RfidDataServiceImpl implements IRfidDataService {

    private final RfidDataMapper baseMapper;

    @Autowired
    private  RfidDataMapper rfidDataMapper;

    /**
     * 查询rfid信息
     */
    @Override
    public RfidDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询rfid信息列表
     */
    @Override
    public TableDataInfo<RfidDataVo> queryPageList(RfidDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RfidData> lqw = buildQueryWrapper(bo);
        Page<RfidDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询rfid信息列表
     */
    @Override
    public List<RfidDataVo> queryList(RfidDataBo bo) {
        LambdaQueryWrapper<RfidData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RfidData> buildQueryWrapper(RfidDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RfidData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMsg()), RfidData::getMsg, bo.getMsg());
        lqw.eq(bo.getDate() != null, RfidData::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getRfidBzm()), RfidData::getRfidBzm, bo.getRfidBzm());
        lqw.eq(StringUtils.isNotBlank(bo.getFromIp()), RfidData::getFromIp, bo.getFromIp());
        return lqw;
    }

    /**
     * 新增rfid信息
     */
    @Override
    public Boolean insertByBo(RfidDataBo bo) {
        RfidData add = BeanUtil.toBean(bo, RfidData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改rfid信息
     */
    @Override
    public Boolean updateByBo(RfidDataBo bo) {
        RfidData update = BeanUtil.toBean(bo, RfidData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RfidData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除rfid信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<RfidDataVo> selectRfidData(String rfidBzm, String fromIp, PageQuery pageQuery) {
        List<RfidDataVo> result = rfidDataMapper.selectRfidData(rfidBzm,fromIp);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<RfidDataVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }
}
